package com.rise.remi.core;

import com.rise.remi.items.Item;
import com.rise.remi.layer.IOManager;
import com.rise.remi.main.Main;
import com.rise.remi.network.Client;
import com.rise.remi.network.Network;
import com.rise.remi.network.Server;
import com.rise.remi.player.Player;
import com.rise.remi.windows.Inventory;
import com.rise.remi.windows.Window;
import com.rise.remi.world.World;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import lombok.Getter;
import lombok.Setter;

/**
 *
 * @author remi
 */
public final class Core_v1 extends JPanel {

    private boolean paintAll = true;
    private @Getter Player player;
    private @Getter Player friend;
    private @Getter World world;
    private @Getter Server server = new Server();
    private @Getter Client client = new Client();
    private @Getter Network network = new Network();
    private ArrayList<Window> windows = new ArrayList<>();
    private Inventory inventory;
    private static Core_v1 instance;
    private @Getter @Setter boolean connected = false;
    private BufferedImage waitingImage;

    public static Core_v1 getInstance() {
        if (instance == null) {
            instance = new Core_v1();
        }
        return instance;
    }

    public void init(String ip, String name, boolean isServer) {
        try {
            waitingImage = ImageIO.read(getClass().getResourceAsStream("/res/waiting.png"));
        } catch (IOException ex) {
        }
        player = new Player("Server", "/res/hero_blurred.png");
        friend = new Player("Client", "/res/32x32_friend.png");
        world = new World(Global.tileWidth, Global.tileHeight, Global.horizontalTiles, Global.verticalTiles);
        try {
            IOManager.newLoadFromFileToVersion2(Global.pathToTheMap, world);
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
        world.getWorldLayer().setBlocksY(-4);
        player.setY(Global.paintWidth / 2 + 3);
        friend.setY(friend.getX() + 3);
        //
        world.getWorldLayer().setBlocksX(-4);
        player.setX(Global.paintWidth / 2 + 4);
        friend.setX(friend.getX() + 4);

        ////////////////////////////////////////////////////////////////////////
        if (isServer) {
            if (!name.isEmpty()) {
                player.setName(name);
            } else {
                player.setName("Server");
            }
            network = server;
            Main.game.setTitle("Server");
            new Thread(server).start();
        } else {
            Network.setSERVER_IP(ip);
            Main.game.setTitle("Client");
            if (!name.isEmpty()) {
                player.setName(name);
            } else {
                player.setName("Client");
            }
            network = client;
            friend.setPaint(true);
            new Thread(client).start();
        }
        Main.addToChat("System: Listen closely.\n\r");
        Main.addToChat("System: You are fag.\n\r");
        ////////////////////////////////////////////////////////////////////////

        inventory = new Inventory("Inventory", Global.tileWidth * 7, Global.tileHeight * 11, getFontMetrics(getFont()).stringWidth("Inventory LOL"));
    }

    public Inventory getInventory() {
        return inventory;
    }

    /**
     *
     * @param isServer true - запуск в режиме сервера, false - запуск в режиме
     * клиента
     */
    @Override
    public void paint(Graphics g) {
        try {
            Thread.sleep(15L);
        } catch (InterruptedException ex) {
        }

        if (paintAll) {
            g.setColor(Color.black);
            g.fillRect(0, 0, Global.windowWidth, Global.windowHeight);
            world.draw(g);
            //paint items
            synchronized (Main.getCore().getWorld().getItems()) {
                for (Item i : world.getItems()) {
                    i.paint(g);
                }
            }
            friend.paint(g, world, true);
            player.paint(g, world, false);

            //windows
            inventory.paint(g);
        }

        if (!isConnected()) {
            final int x = (Main.getFrames()[0].getWidth() >> 1) - (waitingImage.getWidth() >> 1);
            g.drawImage(waitingImage, x, 230, null);
        }

        repaint();
    }

}
